/*
 * @Author: Kaze 3243442830@qq.com
 * @Date: 2025-02-24 19:57:04
 * @LastEditors: Kaze 3243442830@qq.com
 * @LastEditTime: 2025-03-28 10:38:59
 * @FilePath: \vue-position-master\vite.config.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteExternalsPlugin } from 'vite-plugin-externals'
import { insertHtml, h } from 'vite-plugin-insert-html'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import compress from 'vite-plugin-compression'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig(context => {
	const mode = context.mode
	const envDir = 'env'
	const isProd = mode === 'production'
	const env = loadEnv(mode, envDir)
	const olBaseUrl = env['VITE_OL_BASE_URL']
	const cesiumBaseUrl = env['VITE_CESIUM_BASE_URL']
	const olcsBaseUrl = env['VITE_OLCS_BASE_URL']
	const base = '/'

	const resolve = {
		alias: {
			'@': path.resolve(__dirname, './src'),
			'@configs': path.resolve(__dirname, './configs'),
		},
	}

	const plugins = [
		vue(),
		splitVendorChunkPlugin(),
		viteExternalsPlugin({
			cesium: 'Cesium',
		}),
		insertHtml({
			head: [
				h('script', {
					src: isProd ? `${cesiumBaseUrl}Cesium.js` : `${base}${cesiumBaseUrl}Cesium.js`,
				}),
				h('link', {
					rel: 'stylesheet',
					href: isProd ? `${cesiumBaseUrl}Widgets/widgets.css` : `${base}${cesiumBaseUrl}Widgets/widgets.css`,
				}),
				h('link', {
					rel: 'stylesheet',
					href: isProd ? `${olBaseUrl}ol.css` : `${base}${olBaseUrl}ol.css`,
				}),
				h('link', {
					rel: 'stylesheet',
					href: isProd ? `${olcsBaseUrl}olcs.css` : `${base}${olcsBaseUrl}olcs.css`,
				}),
			],
		}),
		AutoImport({
			resolvers: [ElementPlusResolver()],
		}),
		Components({
			resolvers: [ElementPlusResolver()],
		}),
	]
	if (!isProd) {
		const cesiumLibraryRoot = 'node_modules/cesium/Build/CesiumUnminified/'
		const cesiumLibraryCopyToRootPath = cesiumBaseUrl
		const cesiumStaticSourceCopyOptions = ['Assets', 'ThirdParty', 'Workers', 'Widgets'].map(dirName => {
			return {
				src: `${cesiumLibraryRoot}${dirName}/*`,
				dest: `${cesiumLibraryCopyToRootPath}${dirName}`,
			}
		})
		plugins.push(
			viteStaticCopy({
				targets: [
					{
						src: `${cesiumLibraryRoot}Cesium.js`,
						dest: cesiumLibraryCopyToRootPath,
					},
					...cesiumStaticSourceCopyOptions,
				],
			})
		)
		const olcsLibraryRoot = 'node_modules/olcs'
		const olcsLibraryCopyToRootPath = 'libs/olcs2.16.0/'
		plugins.push(
			viteStaticCopy({
				targets: [
					{
						src: `${olcsLibraryRoot}/olcs.css`,
						dest: olcsLibraryCopyToRootPath,
					},
				],
			})
		)
	}
	// gzip压缩
	plugins.push(
		compress({
			threshold: 10 * 1024,
		})
	)

	return {
		base,
		envDir,
		mode,
		plugins,
		resolve,
		server: {
			proxy: {
				'^/map/.*': {
					target: 'http://localhost:8084',
					changeOrigin: true,
					secure: false,
					rewrite: (path) => path.replace(/^\/map/, '/map'),
					configure: (proxy, options) => {
						proxy.on('error', (err, req, res) => {
							console.log('代理错误:', err);
						});
						proxy.on('proxyReq', (proxyReq, req, res) => {
							console.log('代理请求:', req.url, '到', options.target + req.url);
						});
						proxy.on('proxyRes', (proxyRes, req, res) => {
							console.log('代理响应:', proxyRes.statusCode, req.url);
						});
					}
				}
			}
		}
	}
})


